/*
 * Copyright (C) 2004-2006 nickyb@users.sourceforge.net
 * 
 * This file is part of FreeQueryBuilder (http://querybuilder.sourceforge.net)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package net.sourceforge.querybuilder;

import java.awt.GridLayout;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import javax.swing.border.LineBorder;

public class MaskConnect extends BaseMask
{
	MainWindow owner;
	
	JTextField txtDrv;
	JTextField txtUrl;
	JTextField txtUid;

	JPasswordField txtPwd;

	public MaskConnect(MainWindow owner)
	{
		super("open/change connection");
        this.owner = owner;
        
		JPanel pnlLabels = new JPanel(new GridLayout(0,1,3,3));
		JPanel pnlValues = new JPanel(new GridLayout(0,1,3,3));
        
		//jdbc for sqlserver
//		txtDrv = new JTextField("sun.jdbc.odbc.JdbcOdbcDriver");
//		txtUrl = new JTextField("jdbc:odbc:Northwind");
//		txtUid = new JTextField("sa");
//		txtPwd = new JPasswordField();

		//for mysql
		txtDrv = new JTextField("com.mysql.jdbc.Driver");
		txtUrl = new JTextField("jdbc:mysql://192.168.1.104:3306/openfire?useUnicode=true&characterEncoding=UTF8");
		txtUid = new JTextField("senyint");
		txtPwd = new JPasswordField("^YHNJI(");
		
		txtDrv.setBorder(LineBorder.createGrayLineBorder());
		txtUrl.setBorder(LineBorder.createGrayLineBorder());
		txtUid.setBorder(LineBorder.createGrayLineBorder());
		txtPwd.setBorder(LineBorder.createGrayLineBorder());
        
        pnlLabels.add(new JLabel("driver:"	, 4));
        pnlLabels.add(new JLabel("url:"		, 4));
        pnlLabels.add(new JLabel("user:"	, 4));
        pnlLabels.add(new JLabel("password:", 4));
        
        pnlValues.add(txtDrv);
        pnlValues.add(txtUrl);
        pnlValues.add(txtUid);
        pnlValues.add(txtPwd);
        
		setComponentCenter(pnlValues);
		setComponentWest(pnlLabels);
    }
    
	protected void onShowing(){return;}
	protected boolean onConfirm()
	{
        try
        {
			owner.openConnection(txtDrv.getText(),txtUrl.getText(), txtUid.getText(), String.valueOf(txtPwd.getPassword()));
			return true;
        }
        catch(Exception e)
        {
        	e.printStackTrace();
            JOptionPane.showMessageDialog(this, e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
            return false;
        }
	}
}